package com.example.schedulecard.Databases;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import androidx.annotation.NonNull;

import com.example.schedulecard.Entity.Course;
import com.example.schedulecard.Entity.Schedule;

import java.util.ArrayList;
import java.util.List;

public class CourseDatabaseOption {
//    创建课程数据库
    public static CourseDatabase CreateCoursetable(Context context) {
        return new CourseDatabase(context);
    }

//    添加课程信息进入数据库
    public static int addCoure(@NonNull SQLiteDatabase sqLiteDatabase, @NonNull Course course) {
//        方式一：采用键值对的方式进行插入数据
        ContentValues values = new ContentValues();
        values.put("course_name", course.getCourseName());
        values.put("teacher", course.getTeacher());
        values.put("class_room", course.getClassRoom());
        values.put("day", course.getDay());
        values.put("class_start", course.getClassStart());
        values.put("class_end", course.getClassEnd());
        values.put("schedule_name", course.getScheduleName());
        values.put("color",course.getColor());
        return (int) sqLiteDatabase.insert("course", null, values);

//        方式二：采用数据库SQL语句插入,该方法不能返回是否插入成功
//        String sql = "insert into course (course_name,teacher,class_room,day,class_start,class_end,schedule_name,color)" +
//                "values(?, ?, ?, ?, ?, ?, ?, ?)";
//        sqLiteDatabase.execSQL(sql,new String[]{
//                course.getCourseName(),
//                course.getTeacher(),
//                course.getClassRoom(),
//                course.getDay()+"",
//                course.getClassStart()+"",
//                course.getClassEnd()+"",
//                course.getScheduleName(),
//                course.getColor() + ""
//        });
    }

//    删除课程
    public static int deletecourse(SQLiteDatabase sqLiteDatabase, Course course) {
//        方式一
        String sql = "course_name=? and teacher=? and class_room=? and day=? " +
                "and class_start=? and class_end=? and schedule_name=?";
        return (int)sqLiteDatabase.delete("course", sql
                , new String[]{course.getCourseName(),
                        course.getTeacher(),
                        course.getClassRoom(),
                        course.getDay() + "",
                        course.getClassStart() + "",
                        course.getClassEnd() + "",
                        course.getScheduleName()
//                        删除课程不需要将颜色考虑到查找范围也能准确查找,因此不需要考虑这个属性
//                        course.getColor() + ""
                });

//        方式二
//        String sql = "delete from courses where course_name = ? and teacher=? and class_room=? " +
//                "and day =? and class_start=? and class_end=? and schedule_name=? and color=?";
//        sqLiteDatabase.execSQL(sql,new String[]{
//                course.getCourseName(),
//                course.getTeacher(),
//                course.getClassRoom(),
//                course.getDay()+"",
//                course.getClassStart()+"",
//                course.getClassEnd()+"",
//                course.getScheduleName(),
//                course.getColor() + ""
//        });
    }
//    删除表
    public static int deleteschedule(SQLiteDatabase sqLiteDatabase, Schedule schedule) {
        return (int)sqLiteDatabase.delete("course", "schedule_name=?"
                , new String[]{schedule.getSchedulename()});
    }

//    查询所有课程信息
    public static List<Course> querycourse(SQLiteDatabase sqLiteDatabase,String scheduleName) {
        List<Course> courseList = new ArrayList<>();
        Cursor cursor = sqLiteDatabase.rawQuery("select * from course where schedule_name=?"
                , new String[]{scheduleName});
//        遍历课程信息
        while (cursor.moveToNext()) {
            courseList.add(new Course(
                    cursor.getString((int) cursor.getColumnIndex("course_name")),
                    cursor.getString((int) cursor.getColumnIndex("teacher")),
                    cursor.getString((int) cursor.getColumnIndex("class_room")),
                    cursor.getInt((int) cursor.getColumnIndex("day")),
                    cursor.getInt((int) cursor.getColumnIndex("class_start")),
                    cursor.getInt((int) cursor.getColumnIndex("class_end")),
                    cursor.getString((int) cursor.getColumnIndex("schedule_name")),
                    cursor.getInt((int) cursor.getColumnIndex("color"))
            ));
        }
        cursor.close();
        return courseList;
    }

//    更新课程信息
    public static int updatecourse(SQLiteDatabase sqLiteDatabase, Course oldcourse, Course course) {
        ContentValues values = new ContentValues();
//        存储新信息
        values.put("course_name", course.getCourseName());
        values.put("teacher", course.getTeacher());
        values.put("class_room", course.getClassRoom());
        values.put("day", course.getDay());
        values.put("class_start", course.getClassStart());
        values.put("class_end", course.getClassEnd());
        values.put("schedule_name", course.getScheduleName());
//        修改课程不需要将颜色考虑到查找范围也能准确查找,因此不需要考虑这个属性
//        values.put("color", course.getColor());
//        修改原来信息为更改后的信息
        String sql = "course_name=? and teacher=? and class_room=? and day=? and class_start=? " +
                "and class_end=? and schedule_name=?";
        return (int)sqLiteDatabase.update("course", values, sql,
                new String[]{oldcourse.getCourseName(),
                        oldcourse.getTeacher(),
                        oldcourse.getClassRoom(),
                        oldcourse.getDay() + "",
                        oldcourse.getClassStart() + "",
                        oldcourse.getClassEnd() + "",
                        oldcourse.getScheduleName()
//                        course.getColor() + ""
                });
    }
}
